package com.tanhua.server.controller;


import com.tanhua.server.service.PeachBlossomService;
import com.tanhua.server.vo.PeachBlossomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName PeachBlossomController
 * @Description TODO 桃花传音Controller层
 * @Author JavaDev
 * @Date 2021/1/18 8:47
 * @Version 1.0
 */
@RestController
@RequestMapping("peachblossom")
@Slf4j
public class PeachBlossomController {
    @Autowired
    private PeachBlossomService peachBlossomService;

    @PostMapping
    public ResponseEntity savePeachBlossom(
            @RequestParam("soundFile") MultipartFile soundFile) {
        System.out.println("----------------------------------------------");

        try {
            peachBlossomService.savePeachBlossom(soundFile);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error("桃花传音上传失败", e);

        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 桃花传音-接收语音（学生实战）
     *
     * @return
     */
    @GetMapping
    public ResponseEntity queryPeachBlossom(
    ) {
        System.out.println("----------------------------------------------");

        PeachBlossomVo peachBlossomVo = peachBlossomService.queryPeachBlossom();
        if (peachBlossomVo == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok(peachBlossomVo);
    }
}